#include "Player.h"
#include <iostream>


Player::Player() {
}


Player::~Player() {
}
	

void Player::Initialize(sf::RenderWindow* window) {
	m_renderWindow = window;
	m_hp = 50;
	m_score = 0;
	m_playerTexture = new sf::Texture();
	m_playerTexture->loadFromFile("Assets/Images/PLAYER.png");
	m_playerSprite = new sf::Sprite();
	m_playerSprite->setTexture(*m_playerTexture);
	m_clock = new sf::Clock();
	m_playerSprite->scale(0.8f,0.8f);
	m_playerSprite->setOrigin(m_playerTexture->getSize().x/2, m_playerTexture->getSize().y);
	m_playerSprite->setPosition(m_renderWindow->getSize().x/2, m_renderWindow->getSize().y);

	m_collisionRect = new sf::RectangleShape();
	m_collisionRect->setSize(sf::Vector2f(m_playerSprite->getGlobalBounds().width - 60.f, m_playerSprite->getGlobalBounds().height - 30.f));
	sf::Color color;
	color.a = 100;
	color.r = 255;
	m_collisionRect->setFillColor(color);
	m_collisionRect->setOrigin(m_collisionRect->getSize().x/2,m_collisionRect->getSize().y);

	m_hurtBuffer = new sf::SoundBuffer();
	m_hurtBuffer->loadFromFile("Assets/Sounds/hurt.wav");
	m_hurtSound = new sf::Sound();
	m_hurtSound->setBuffer(*m_hurtBuffer);
	m_hurtSound->setVolume(60.f);
	m_lastHp = m_hp;
}


void Player::Update() {
		m_collisionRect->setPosition(m_playerSprite->getPosition());
		m_collisionRect->setRotation(m_playerSprite->getRotation());	

	if(m_score < 0){
		m_score = 0;
	}
	if(m_hp < m_lastHp){
		m_hurtSound->play();
		}
	timeSinceLastFrame = m_clock->restart();
//	std::cout << m_hp << std::endl;
	m_renderWindow->draw(*m_playerSprite);
	//m_renderWindow->draw(*m_collisionRect);
	std::cout << m_collisionRect->getPosition().x << std::endl;
		m_lastHp = m_hp;

		if(m_playerSprite->getPosition().x > 960.f){		
			m_playerSprite->setRotation(m_playerSprite->getPosition().x * -0.1f + 101.f );
		}

		if(m_playerSprite->getPosition().x < 960.f){		
			m_playerSprite->setRotation(m_playerSprite->getPosition().x * -0.1f + 101.f );
		}

	/*
	if(m_playerSprite->getPosition().x < 300.f){
		m_playerSprite->setPosition(301.f, m_playerSprite->getPosition().y);
	}
	if( m_playerSprite->getPosition().x > 950.f){
		m_playerSprite->setPosition(949.f, m_playerSprite->getPosition().y);
	}
*/
}


void Player::Cleanup() {
	delete m_collisionRect;
	delete m_playerSprite;
	delete m_playerTexture;
	delete m_clock;
	delete m_hurtSound;
	delete m_hurtBuffer;
}


void Player::move(float xDir, float yDir)
{
	
	float speedX = xDir * 400.0f * timeSinceLastFrame.asSeconds();
	float speedY = yDir * 400.0f * timeSinceLastFrame.asSeconds();

	m_lastPos = m_playerSprite->getPosition(); 
	m_playerSprite->move(speedX, speedY);

}